package com.zhongke.cotrun.model.exception;


import com.aliyuncs.exceptions.ClientException;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;


@RestControllerAdvice
public class AllException {


    /**
     * 参数校验异常
     *
     * @param
     * @return
     */
    @ExceptionHandler({BindException.class})
    public APIResponse parameterValidator(BindException e) {
        List<ObjectError> errors = e.getAllErrors();
        ObjectError error = errors.get(0);
        String msg = error.getDefaultMessage();
        String code = error.getCode();
        return APIResponse.returnFail(CodeMsg.BIND_ERROR.fillArgs(msg));
    }


    @ExceptionHandler({ConstraintViolationException.class})
    public APIResponse parameterValidator(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> errors = e.getConstraintViolations();

        String message = errors.iterator().next().getMessage();
        CodeMsg codeMsg = new CodeMsg(500104, message);
        return APIResponse.returnFail(codeMsg);
    }

    @ExceptionHandler(value = UnauthorizedException.class)
    public APIResponse shiroErrorHandler(UnauthorizedException e) {
        return APIResponse.returnFail(CodeMsg.AUTH_ERROR);
    }

    @ExceptionHandler(value = UnauthenticatedException.class)
    public APIResponse shiroErrorHandler(UnauthenticatedException e) {
        return APIResponse.returnFail(CodeMsg.SESSION_ERROR);
    }

    @ExceptionHandler(value = AuthorizationException.class)
    public APIResponse shiroErrorHandler(AuthorizationException e) {
        return APIResponse.returnFail(CodeMsg.AUTH_ERROR);
    }


    /**
     * 自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    public APIResponse<Object> myError(MyException e) {

        return APIResponse.returnFail(e.getCodeMsg());

    }

    @ExceptionHandler(RRException.class)
    public APIResponse<Object> myError(RRException e) {
        return APIResponse.returnFail(new CodeMsg(e.getCode(),e.getMsg()));
    }

    /**
     * 数字限制
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NumberFormatException.class)
    public APIResponse numberException(NumberFormatException e) {
        return APIResponse.returnFail(CodeMsg.NUMBER_ERROP);
    }

    /**
     * 短信异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ClientException.class)
    public APIResponse clientexception(ClientException e) {
        return APIResponse.returnFail(CodeMsg.MESSAGE_ERROR);
        //return ReturnResult.erro(CodeMsg.MESSAGE_ERROR);
    }

}
